package com.byzx.wine.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.byzx.wine.po.Syrole;
import com.byzx.wine.service.IRoleService;
import com.byzx.wine.util.Page;

@Controller
@RequestMapping(value = "/role")
public class RoleAction {
	@Autowired
	private IRoleService roleService;

	@RequestMapping("role1")
	public String login(HttpServletResponse response, HttpServletRequest request) {
		return "role/role";
	}

	@RequestMapping("findrole")
	@ResponseBody
	public Map<String, Object> findPage(Model model, Integer iDisplayLength,
			Integer sEcho, Integer iDisplayStart) {
		if (sEcho == null) {
			sEcho = 1;
		}
		if (iDisplayLength == null) {
			iDisplayLength = 10;
		}
		if (iDisplayStart == null) {
			iDisplayStart = 1;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(
				iDisplayStart, iDisplayLength, map);
		Page<Map<String, Object>> page1 = roleService.findPage(page);

		map.put("sEcho", sEcho);
		map.put("iTotalRecords", page1.getTotalCount());
		map.put("iTotalDisplayRecords", page1.getTotalCount());
		map.put("aaData", page1.getList());
		return map;
	}

	@RequestMapping("roleadd")
	@ResponseBody
	public Map<String, Object> roleadd(Syrole role) {
		System.out.println(role.getRolename());
		Map<String, Object> map = new HashMap<String, Object>();
		int num = roleService.insert(role);
		map.put("num", num);
		return map;
	}
	
	@RequestMapping("/selectByPrimaryKey")
	@ResponseBody
	public Map<String,Object> selectByPrimaryKey(Integer id){
			return roleService.selectByPrimaryKey(id);
	}

	@RequestMapping("/roleupdate")
	@ResponseBody
	public Map<String,Object> roleupdate(Syrole role){
		
		Map<String,Object> map =new HashMap<String, Object>();
		int num=roleService.updateByPrimaryKey(role);
		map.put("num", num);
	return map;
	}

	@RequestMapping("/roledelete")
	@ResponseBody
	public Map<String, Object> roledelete(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		int num = roleService.deleteByPrimaryKey(id);
		map.put("num", num);
		return map;
	}

	@RequestMapping("/roledeletes")
	@ResponseBody
	public Map<String, Object> roledeletes(String ids) {
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("flag", roleService.deleteAll(ids));
		return map;
	}

}
